package com.jxnu.duewang.zmly.zmly.adapter;

import android.content.Context;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.viewpager.widget.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jxnu.duewang.zmly.BR;
import com.jxnu.duewang.zmly.R;
import com.jxnu.duewang.zmly.zmly.model.IntroduceImageLastEvent;

import java.util.ArrayList;

/**
 * Project Zmly
 * Author Duewang
 * Time 2016/12/4 0004
 */

public class IntroducePictureAdapter extends PagerAdapter{

    private Context mContext;
    private ArrayList<String> mImageUrl;
    private IntroduceImageLastEvent mIntroduceImageLastEvent;

    public IntroducePictureAdapter(Context context, ArrayList<String> imageUrl,IntroduceImageLastEvent introduceImageLastEvent) {
        mContext = context;
        mImageUrl = imageUrl;
        mIntroduceImageLastEvent=introduceImageLastEvent;
    }

    @Override
    public int getCount() {
        int ret=0;
        if (mImageUrl!=null) {
            ret=mImageUrl.size();
        }
        return ret;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View ret;
        if(position==mImageUrl.size()-1){
            ret=DataBindingUtil.inflate(LayoutInflater.from(mContext),R.layout.introduce_image_last,container,false).getRoot();
            ViewDataBinding binding = DataBindingUtil.bind(ret);
            binding.setVariable(BR.imgLast,mImageUrl.get(position));
            binding.setVariable(BR.imgLastEvent,mIntroduceImageLastEvent);
        }else{
            ret = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.introduce_image, container, false).getRoot();
            ViewDataBinding binding = DataBindingUtil.bind(ret);
            binding.setVariable(BR.img,mImageUrl.get(position));
        }
        container.addView(ret);
        return ret;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View)object);
    }
}
